package router

import (
	"im/middleware"
	"im/service"

	"github.com/gin-gonic/gin"
)

func Router() *gin.Engine {
	apiService := service.AppService
	e := gin.Default()
	v1 := e.Group("/v1")
	// 注册路由
	us := apiService.UserService
	{
		v1.GET("login", us.Login)
	}

	// gin.BasicAuth()
	authorized := e.Group("v1", middleware.JWTAuth())
	{
		authorized.GET("logout", us.Logout)
		userRouter := authorized.Group("user")
		{
			// 用户
			userRouter.GET("list", us.UserList)
			userRouter.GET("create", us.UserCreate)
			userRouter.GET("del", us.UserDelete)
			userRouter.PUT("update", us.UserUpdate)
			userRouter.GET(":id", us.Detail)
			userRouter.GET("selectByPhone", us.UserSelectByPhone)
			userRouter.GET("selectByName", us.UserSelectByName)
		}

		// 群
		cgs := apiService.ChatGroupService
		groupRouter := authorized.Group("group")
		{
			groupRouter.POST("add", cgs.AddGroup)
			groupRouter.PUT("update", cgs.UpdateGroup)
			groupRouter.PUT("list", cgs.ListGroups)
			groupRouter.PUT("del", cgs.DelGroup)
		}

		// 群成员
		cgms := apiService.ChatGroupMemberService
		memberRouter := authorized.Group("member")
		{
			memberRouter.POST("add", cgms.AddMember)
			memberRouter.PUT("del", cgms.DelMember)
			memberRouter.PUT("list", cgms.ListMember)
		}

		// 好友
		ucs := apiService.UserContactService
		contactRouter := authorized.Group("contact")
		{
			contactRouter.PUT("add", ucs.AddContact)
			contactRouter.PUT("del", ucs.DelContact)
			contactRouter.PUT("list", ucs.ListContacts)
		}

		cs := apiService.ChatService
		chatRouter := authorized.Group("/ws")
		{
			// hub := chat.CreateHub()
			chatRouter.GET("/", cs.WsHandler)
		}
	}
	return e
}
